package com.fanco.learn_datastruct_leetcode.datastruts.lru01;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author qiaowang 2022/3/8
 * 描述
 */
public class LRUCacheDemo<K,V> extends LinkedHashMap<K,V> {

    private int capacity; // 缓存容量

    /**
     * true : 访问顺序
     * false: 插入顺序
     * @param capacity
     */
    public LRUCacheDemo(int capacity) {
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LRUCacheDemo lruCacheDemo = new LRUCacheDemo(3);
        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(4, "d");
        System.out.println(lruCacheDemo.keySet());

    }
}
